package com.mibao.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.mipai.R;

/**
 * Created by tianweishu on 2014/10/27.
 */
public class ContactsAdapter extends BaseAdapter {

    private LayoutInflater inflater;

    private List<ConatctListItem> conatctListItem;

    public ContactsAdapter(Context context, List<ConatctListItem> conatctListItem) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.conatctListItem = conatctListItem;
    }

    @Override
    public int getCount() {
        return conatctListItem.size();
    }

    @Override
    public Object getItem(int i) {
        return conatctListItem.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {

        ViewHolder viewHolder;

        if (convertView == null || convertView.getTag() == null) {
            viewHolder = new ViewHolder();

            convertView = inflater.inflate(R.layout.contacts_list_item, null);
            viewHolder.dot = convertView.findViewById(R.id.contacts_dot);
            viewHolder.name = (TextView) convertView.findViewById(R.id.contacts_name);
            viewHolder.sms = convertView.findViewById(R.id.contacts_sms);
            viewHolder.phone = convertView.findViewById(R.id.contacts_phone);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        ConatctListItem item = conatctListItem.get(i);
        if (item.getUnreadPhone() > 0 || item.getUnreadSms() > 0){
            // 有未读短信或者电话，那么显示点点点
            viewHolder.dot.setVisibility(View.VISIBLE);
        } else {
            viewHolder.dot.setVisibility(View.INVISIBLE);
        }

        viewHolder.name.setText(item.getName());

        viewHolder.sms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // todo send sms
            }
        });

        viewHolder.phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // todo call phone
            }
        });

        return convertView;
    }

    public static class ViewHolder{
        View dot;
        TextView name;
        View sms;
        View phone;
    }

    public static class ConatctListItem{
        // 名字
        private String name;

        //电话号码
        private String number;

        // 未读短信条数
        private int unreadSms;

        // 未读电话条数
        private int unreadPhone;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getNumber() {
            return number;
        }

        public void setNumber(String number) {
            this.number = number;
        }

        public int getUnreadSms() {
            return unreadSms;
        }

        public void setUnreadSms(int unreadSms) {
            this.unreadSms = unreadSms;
        }

        public int getUnreadPhone() {
            return unreadPhone;
        }

        public void setUnreadPhone(int unreadPhone) {
            this.unreadPhone = unreadPhone;
        }
    }
}
